package com.example.springbootgitlab.controller;

import com.alibaba.fastjson.JSON;
import org.gitlab4j.api.GitLabApi;
import org.gitlab4j.api.GitLabApiException;
import org.gitlab4j.api.models.Project;
import org.gitlab4j.api.models.RepositoryFile;
import org.gitlab4j.api.models.TreeItem;
import org.gitlab4j.api.models.User;

import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class Test {

    public static void main(String[] args) {
        try {
            GitLabApi gitLabApi = new GitLabApi("http://192.168.243.34:8880", "UBz6zyS7YNWdGm-8uqLb");
            //gitLabApi.sudo("root");
            List<TreeItem> trees = gitLabApi.getRepositoryApi().getTree(15,"/", "master",true);
            HashMap<String ,Integer> myFilemap = new HashMap<String ,Integer>();
            for (TreeItem item:trees) {
                System.out.println(JSON.toJSONString(item));
                //List<TreeItem> subTree = getSubTree(gitLabApi, 15, item.getPath(),totalSize);
                if (item.getType() == TreeItem.Type.BLOB) {
                    String fileName = item.getName();
                    // 没有扩展名的情况
                    String suffix = "notype";
                    //获取扩展名
                    if (fileName.lastIndexOf('.') >= 0) {
                        suffix = fileName.substring(fileName.lastIndexOf('.' )+1);
                    }else {
                        System.out.println(item.getPath());
                    }
                    // 如果该类型已经存在于map中
                    // 则原有的基础之上自增
                    // 否则创建新的对象保存在map中
                    if (myFilemap.get(suffix)==null){
                        myFilemap.put(suffix,1);
                    }else{
                        Integer typeCount = myFilemap.get(suffix);
                        typeCount ++ ;
                        myFilemap.put(suffix,typeCount);
                        //System.out.println(suffix+":"+typeCount);
                    }
                }
            }
            System.out.println(JSON.toJSONString(myFilemap));
         }catch (GitLabApiException e) {
            e.printStackTrace();
        }

    }

    private static List<TreeItem> getSubTree(GitLabApi gitLabApi ,int projectId, String path,int totalSize){
        try{
            System.out.println("----subIitem------" + path);
            List<TreeItem> trees = gitLabApi.getRepositoryApi().getTree(15, path, "master");
            for (TreeItem subItem : trees) {
                if (subItem.getType() == TreeItem.Type.TREE) {
                    //如果是文件夹的话，就打开文件夹
                    getSubTree(gitLabApi,15,subItem.getPath(),totalSize);
                }else {
                    //如果是文件的话
                    RepositoryFile repositoryFile = gitLabApi.getRepositoryFileApi().getFileInfo(15, subItem.getPath(), "master");
                    System.out.println("file-----:"+repositoryFile.getFileName()+":"+repositoryFile.getSize());
                    totalSize = totalSize + repositoryFile.getSize();
                }
            }
            return trees;
        }catch (GitLabApiException e) {
            e.printStackTrace();
        }
        return null;

    }


    public static void test1(String[] args) {
        try {
            GitLabApi gitLabApi = new GitLabApi("http://192.168.243.34:8880", "UBz6zyS7YNWdGm-8uqLb");
            //gitLabApi.sudo("root");
            Stream<User> stream = gitLabApi.getUserApi().getUsersStream();
            List<User> users = stream.parallel().sorted(Comparator.comparing(User::getUsername)).collect(Collectors.toList());
            users.stream().forEach(item -> System.out.println(item.getUsername()));
            System.out.println("--------------------------------------------");
            List<Project> projects = gitLabApi.getProjectApi().getOwnedProjects();
            projects.stream().forEach(item -> System.out.println(item.getName()));
            for (User user: users){
                System.out.println(user.getUsername() + "--------------------------------------------");
                gitLabApi.sudo(user.getUsername());
                List<Project> vicPros = gitLabApi.getProjectApi().getOwnedProjects();
                vicPros.stream().forEach(item -> System.out.println(item.getName()));
                for (Project project: vicPros){
                    if (project.getName().equals("my-qianyi")){

                        List<TreeItem> trees = gitLabApi.getRepositoryApi().getTree(15);
                        for (TreeItem item:trees){
                            System.out.println(JSON.toJSONString(item));
                            if (item.getType() == TreeItem.Type.TREE){
                                List<TreeItem> master = gitLabApi.getRepositoryApi().getTree(15, item.getPath(), "master");
                                for (TreeItem subItem:master){
                                    System.out.println("subIitem------"+JSON.toJSONString(subItem));
                                }
                            }
                        }
                        //gitLabApi.getRepositoryFileApi().getFileInfo()
                        //RepositoryFile file = gitLabApi.getRepositoryFileApi()("docs", 15, "master");
                        //System.out.println(JSON.toJSONString(project));
                    }
                }
                //gitLabApi.unsudo();
            }
        } catch (GitLabApiException e) {
            e.printStackTrace();
        }

    }



}
